local Piece = require "packages.chess-games.xiangqi.core.piece"

local Advisor = Piece:subclass("Xiangqi.Advisor")

function Advisor:initialize(id)
  Piece.initialize(self, id)

  self.name = 'advisor'
  self.redStr = '仕'
  self.blackStr = '士'
end

---@param board integer[][] 表示棋盘的二维数组，内有棋子id或者0
---@param fromX integer
---@param fromY integer
---@param x integer
---@param y integer
function Advisor:canMoveTo(board, fromX, fromY, x, y)
  local dx = x - fromX
  local dy = y - fromY

  -- 必须斜走一步
  if math.abs(dx) ~= 1 or math.abs(dy) ~= 1 then
    return false
  end

  -- 必须在九宫格内
  local myColor = self:getColor()
  if myColor == "red" then
    if x < 4 or x > 6 or y < 8 or y > 10 then
      return false
    end
  else
    if x < 4 or x > 6 or y < 1 or y > 3 then
      return false
    end
  end

  -- 目标位置若有己方棋子，不可走
  local targetId = board[y][x]
  if targetId ~= 0 then
    local targetColor = (targetId > 0) and "red" or "black"
    if myColor == targetColor then
      return false
    end
  end

  return true
end

return Advisor

